package com.gdd.videos.mini_api.common;

import com.gdd.videos.common.common.GddResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName parent
 * @title     ControllerExceptionAdvice   
 * @package    com.gdd.videos.mini_api.common  
 * @date   2019/9/12 15:28  
 * @explain
 */
@Slf4j
@ControllerAdvice
public class ControllerExceptionAdvice {

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public GddResult errorHandler(Exception ex) {

        // 处理上传文件发生的错误
        if (ex instanceof MultipartException){
            // 文件大小超出限制时抛出错误信息
            if (ex.getMessage().contains("FileSizeLimitExceededException")){
                return GddResult.errorMsg("文件大小超出10M限制!");
            }
        }
        return GddResult.errorMsg(ex.getMessage());
    }

}
